#include	<stdio.h>
#include	<stdlib.h>
#include	<stdint.h>
#include	<strings.h>
#include 	"protocol.h"
#include	"types.h"
#include	"player.h"
#include	"object.h"

Object Objects[NUMOBJS];

uint16_t
object_init(Maze *m)
{
	uint16_t objCnt = 0;
	
	uint16_t teamNo;
	for (teamNo=1; teamNo<=NUMTEAMS; teamNo++)
	{
		Object *flagObj = (Object *)malloc(sizeof(Object));
		Object *bombObj = (Object *)malloc(sizeof(Object));
		
		if (flagObj){			
			flagObj->teamNo = teamNo;
			flagObj->state = (teamNo == 1) ? FLAG_ON_TEAM_1 : FLAG_ON_TEAM_2;
			
			if (maze_object_add(m, flagObj)){
				Objects[objCnt] = *flagObj;
				objCnt++;
			}
			else {
				free(flagObj);
			}
		}
		if (bombObj){		
			bombObj->teamNo = teamNo;
			bombObj->state = (teamNo == 1) ? BOMBS_ON_TEAM_1 : BOMBS_ON_TEAM_2;
			
			if (maze_object_add(m, bombObj)){
				Objects[objCnt] = *bombObj;
				objCnt++;
			}
			else {
				free(bombObj);
			}
		}
		printf("object_init: team %d flag and bomb init\n", teamNo);
	}
	//object_dump();
	return 1;
}

uint16_t
object_dump()
{		
	int i;
	for (i=0; i<NUMOBJS; i++){
		Object *o = &Objects[i];
		printf("row=%d, col=%d\n", o->cellPtr->pos.row, o->cellPtr->pos.col);
	}
	return 1;
}

uint16_t
object_toggle_flag_capture(uint16_t teamNo){
	Object *o = (teamNo == 1) ? &Objects[TEAM1_FLAG_ID-1] : &Objects[TEAM2_FLAG_ID-1];
	OBJECT_TOGGLE_FLAG_CAPTURE(o);
	
	return IS_FLAG_CAPTURED(o);
}

uint16_t
object_is_flag_captured(uint16_t teamNo){
	Object *o = (teamNo == 1) ? &Objects[TEAM1_FLAG_ID-1] : &Objects[TEAM2_FLAG_ID-1];
	
	return IS_FLAG_CAPTURED(o);
}

